#include <iostream>
#include <map>
using namespace std;

map<string,int> alumni;
map<string,int> participator;

signed main()
{
	int n,m;
	cin>>n;
	string s;
	for(int i=0;i<n;i++){
		cin>>s;
		alumni[s] = 1;
	}
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>s;
		participator[s] = 1;
	}
	string alumniBirth = "999999999",ans1;
	string participatorBirth = "999999999",ans2;
	int ansNum = 0;
	for(auto it:participator){
		if(alumni.find(it.first)!=alumni.end()){
			ansNum++;
			if(it.first.substr(6,8)<alumniBirth){
				alumniBirth = it.first.substr(6,8);
				ans1 = it.first;
			}
		}
		if(it.first.substr(6,8)<participatorBirth){
			participatorBirth = it.first.substr(6,8);
			ans2 = it.first;
		}
	}
	cout<<ansNum<<"\n";
	if(alumniBirth!="999999999")	cout<<ans1<<"\n";
	else	cout<<ans2<<"\n";
	return 0;
} 
